<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller 
{
    public function login()
    {	
        layout(false);
        $this->display();      
    }

    public function captcha()
    {
    	$config  = array(
    		'fontSize' => 30,
    		'lenght' => 7,
    		);
    	$Verify =   new \Think\Verify($config);
		$Verify->entry();
    }

    // 判断用户名
    public function checkusername()
    {
    	$username = I('post.username','');
		
		if($username){

		$preg = "/^[a-z][a-z0-9]{5,7}$/";

		$c = preg_match($preg, $username);

		if(!$c){
			$arr = array(
				'error'=>1,
				'info'=>'用户名必须是6-8位'
				);
			echo json_encode($arr);
		}else{
			$sql = "select username from user ";

	        $res =M()->query($sql);
			
			foreach ($res as $key => $user) {
				if($user['username']==$username){
					$arr = array(
						'error'=>1,
						'info'=>'用户名已存在'
						);
					echo json_encode($arr);
					exit;
				}			
			}
			$arr = array(
				'error'=>0,
				'info'=>'用户名可以用'
				);
			echo json_encode($arr);
		}
			}else{
		$arr = array(
				'error'=>1,
				'info'=>'用户名不能为空'
				);
			echo json_encode($arr);
			}
	    }

    // 邮箱的判断：
    public function checkmail()
    {
    	$mail = I('post.mail','');
    	
    	if ($mail) {
    		$preg = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
    		$c = preg_match($preg, $mail);
    		if (!$c) {
    			$arr = array(
    				'error' => 1,
    				'info'	=> '邮箱格式不正确'
    				);
    			echo json_encode($arr);
    		}else{
    			$sql = "select email from user";
    			$res =	M()->query($sql);
    			foreach ($res as $key => $val) {
    				if ($val['email']==$mail) {
    					$arr = array(
    						'error'=> 1,
    						'info'=>'邮箱已存在，请登录',
    						);
    					echo json_encode($arr);
    					exit();
    				}
    			}
	        	$arr =array(
	        		'error'=>0,
	        		'info' =>'邮箱可以用'
	        				);
	        			echo json_encode($arr); 
    		}
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info' =>'邮箱不能空',
    			);
    		echo json_encode($arr);
    	}


    }

    public function checkpassword()
    {
    	$password = I('post.password','');

    	if ($password) {
    		$preg = "/^[a-z0-9]{6,33}$/";
    		$c = preg_match($preg, $password);

    		if (!$c) {
    			$arr = array(
    				'error'=>1,
    				'info' =>'密码是不少于6位'
    				);
    			echo json_encode($arr);
    		}else{
    			$arr = array(
    				'error'=>0,
    				'info' =>'密码可以用'
    				);
    			echo json_encode($arr);
    		}
    	}else{
    		$arr = array(
    				'error'=>1,
    				'info' =>'密码不可以为空'
    				);
    			echo json_encode($arr);
    	};
    	
    }

    public function checkcaptcha()
    {
    	$code = I('post.code','');
    	// 避免重置
    	$config =array(
	    	'reset'       =>false,
		);
		$verify = new \Think\Verify($config);
		$res =  $verify->check($code, '');

		if($res){
			$data = array(
			'error'=>0,
			'info'=>'验证码正确'
			);
			echo json_encode($data);
		}else{
			$data = array(
			'error'=>1,
			'info'=>'验证码不正确'
			);
			echo json_encode($data);
		}
    }

    // 将数据推向数据库

    public function add()
    {
    	
    	$data = $_POST;
    	$aa = md5($data['password']);
        $data['password'] = $aa;
    	$res = M('user')->add($data);
    	$this->success('注册成功',U('home/login/login'),1);
    }

    // 用户登录
    public function logon()
    {
    	$str = $_POST['mail'];
		$ps = md5($_POST['password']);
		$res = M()->query("select * from user s1 where s1.email='$str' && s1.password='$ps'");
		if($res){
					session_start();
                    $_SESSION['username']=$res[0]['username'];
                    $_SESSION['id']=$res[0]['id'];
                    $_SESSION['face']=$res[0]['face'];
					$_SESSION['mail']=$_POST['mail'];
					$this->success('登录成功','http://localhost/p201604/seg/seg/',3);
					
				}else{
					$this->error('登录失败，账号或密码错误，请重新输入','http://localhost/p201604/seg/seg/',5);
					exit;
				}
    }

    public function loginout()
    {
        session_unset();
        session_destroy();
        $this->success('退出成功',U('home/index/index'),1);
    }

    // 使用外部接口方式发邮件;
    public function sendmail()
    {
        $url = 'http://127.0.0.1/php/seg/index.php/home/login/getmail';

        $API_USER = 'zhibisdfsdf';
        $API_PWD = 'asfdlkjwlkerjwelkr';

        $param = array(
            'api_user' => $API_USER, # 使用api_user和api_pwd进行验证
            'api_pwd' => $API_PWD,
            'from' => 'support@emailcar.net', # 发信人，用正确邮件地址替代
            'fromname' => 'Emailcar',
            'to' => '113664000@qq.com',# 收件人地址, 用正确邮件地址替代, 多个地址用';'分隔  
            'subject' => '欢迎使用Emailcar',
            'html' => 'Emailcar为您服务！'
        );


        $data = http_build_query($param);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-Type: application/x-www-form-urlencoded',
                'content' => $data
        ));
        $context  = stream_context_create($options);
        $result = file_get_contents($url, FILE_TEXT, $context);

        var_dump($result);
    }

    
    public function getmail()
    {
        $arr = $_REQUEST;
        $arr = array('code'=>'0','info'=>'你没有权限');
        echo json_encode($arr);
    }

    // 使用腾讯免费发送邮件
    public function sendqqmail()
    {   
        $str = $_POST['mail'];
        // echo ($str);exit();
        $url = "http://localhost/p201604/seg/seg/index.php/home/user/checkmail";
        qqmail( $str,'carson',"这个是一个更改邮箱登陆的邮件$url");
        $this->success('邮件发送成功',U('home/user/email'),3);

        
    }
    


}?>